﻿@model AntUnion.FrameWork.Model.Models.TbRole
@{
    ViewData["Title"] = "Create";
    Layout = "~/Areas/Console/Views/Shared/_Layout.cshtml";
}
@section head{
    <script type="text/javascript">
        layui.config({
            base: '../../layuiadmin/' //静态资源所在路径
        }).extend({
            treeSelect: 'modules/treeSelect/treeSelect',
            index: 'lib/index'
        }).use(['treeSelect', 'index', 'table', 'form'], function () {
            var $ = layui.$
                , form = layui.form
                , admin = layui.admin
                , table = layui.table
                , treeSelect = layui.treeSelect
                , parentFrameIndex = parent.layer.getFrameIndex(window.name); //获取窗口索引;

            /**多语言初始化 */
            top.SetFormPageLanguage(layui);

            var CheckedId = $("#Pid").val();
            var userType = $("#UserType").val();
            $.getJSON("/language/" + language + ".json", function (lang) {
                for (var key in lang) {
                    if (lang.hasOwnProperty(key)) {
                        if (key === 'topLevel') {
                            var text = lang[key];
                            renderTree(text);
                        }
                    }
                }
            });
            renderTree = function (text) {
                treeSelect.render({
                    elem: '#SelectTree',
                    data: '/Console/Roles/GetTreeJson?userType=' + userType + '&topName=' + text + '&CheckedId=' + CheckedId,
                    type: 'get',
                    placeholder: text,
                    search: true,
                    click: function (data) {
                        $("#Pid").val(data.current.id);
                    },
                    success: function (res) {
                        var item = res.data;
                        var selectid = 0;
                        for (var i = 0; i < item.length; i++) {
                            if (item[i].checked == true) {
                                selectid = item[i].id;
                                break;
                            } else {
                                var children = item[i].children;
                                for (var j = 0; j < children.length; j++) {
                                    if (children[j].checked == true) {
                                        selectid = children[j].id;
                                        break;
                                    }
                                }
                            }
                        }
                        if (selectid != 0) {
                            treeSelect.checkNode('SelectTree', selectid);
                        }
                    }
                });
            };
            /* 监听提交 */
            form.on('submit(submit-form)', function (data) {
                if ($("#Names").val() == "") {
                    top.AlertByKey(layui, "enterRolesName", 0);
                    return;
                }
                $.ajax({
                    type: "POST",
                    url: "/Console/Roles/Create",
                    data: data.field, // 你的formid
                    async: true,
                    error: function (request) {
                        alert("Connection error");
                    },
                    beforeSend: function (xhr) {
                        layui.layer.load();
                    },
                    success: function (data) {
                        var item = eval(data);
                        if (item.code == 0) {
                            /**提示语弹框 */
                            top.ShowMessageByKey(layui, "submitSuccess", 6, 1000, function () {
                                parent.layer.close(parentFrameIndex);
                                parent.renderTable();
                            });
                        } else {
                            /**提示语弹框 */
                            top.AlertByKey(layui, "submitFail", 0);
                        }
                        layer.closeAll('loading');
                    }
                });
                return false;
            });

        });
    </script>
}

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body" style="padding: 15px;">
            <form class="layui-form" action="" lay-filter="component-form-group">
                <div class="layui-form-item" style="display:none;">
                    <label class="layui-form-label" lay-event="superiorRoles">上级角色</label>
                    <div class="layui-input-block">
                        <input type="text" id="SelectTree" name="SelectTree" lay-filter="SelectTree" class="layui-input">
                    </div>
                    <div style="color:red;margin-left:110px;" lay-event="rolesAttentionalCueing">注：不选择为顶级</div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label" lay-event="rolesName">角色名</label>
                    <div class="layui-input-block">
                        <input type="text" name="Names" id="Names" placeholder="enterRolesName" autocomplete="off" class="layui-input">
                        @Html.HiddenFor(m => m.Level)
                        @Html.HiddenFor(m => m.Pid)
                        @Html.HiddenFor(m => m.UserType)
                    </div>
                </div>
                <div class="layui-form-item layui-form-text">
                    <label class="layui-form-label" lay-event="remark">备注</label>
                    <div class="layui-input-block">
                        <textarea name="Remark" id="Remark" placeholder="remark" class="layui-textarea">@Model.Remark</textarea>
                    </div>
                </div>
                <div class="layui-form-item layui-layout-admin">
                    <div class="layui-input-block">
                        <div class="layui-footer" style="left: 0;">
                            <button id="submitForm" class="layui-btn" lay-submit="" lay-filter="submit-form">立即提交</button>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>